/**
 * Universita' degli Studi di Palermo, 2010-11, License GNU GPL 3 (http://www.gnu.org/licenses/gpl.html)
 */
package org.unipa.botplatform.util;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Static class used to manage files.
 * @author Giuseppe Miceli
 *
 */
public class FileManager {

	/**
	 * 
	 * @param relativePath
	 * @return
	 */
	public static String getValidAbsolutePath(String relativePath){
		String dir = System.getProperty("user.dir");
		if (!dir.startsWith("/")) dir = "/" + dir;
		String validPath = "file://" + dir + "/" + relativePath;
		return validPath;
	}

	/**
	 * Tries to make a valid URL string starting from the passed path. It tries to understand the kind of path using simple empirical heuristics.
	 * @param relativePath
	 * @return
	 */
	public static String getValidURLString(String path){
		String dir = System.getProperty("user.dir");
		if (!dir.startsWith("/")) dir = "/" + dir;
		String validPath = "file://" + dir + "/" + path;
		return validPath;
	}

	public static URL getValidURL(String path) throws MalformedURLException{
		if (FileManager.class.getResource(path) != null){
			return FileManager.class.getResource(path);
		}

		if (path.startsWith("file:")){
			return new URL(path);
		}

		String dir = System.getProperty("user.dir");
		if (!dir.startsWith("/")) dir = "/" + dir;
		String validPath = "file://" + dir + "/" + path;
		return new URL(validPath);
	}
}
